blog

home / developersection / blogs / interface in c#

Interface in C#

Uttam Misra 3774 27-Dec-2010

An interfacelooks like a class, but has no implementation. The only thing it contains isdefinitions of events, indexers, methods and/or properties.The reason interfaces onlyprovide definitions is because they are inherited by classes and structs,which must provide an implementation for each interface member defined.

Example
//defining first interface
    interface Ifirst
    {
        voidsum(int a, intb);
    }
 
//definingsecond interface which inherits first interface
    interface Isecond:Ifirst
    {
        voidsub(int a, intb);
    }
 
//definingclass which inherits second interface
 
    class newClass : Isecond
    {
//implementinginterface defined methods
        public void sum(int a, int b)
        {
            MessageBox.Show("Sum is: " + (a + b).ToString());
        }
        public void sub(int a, int b)
        {
            MessageBox.Show("Difference is: " + (a - b).ToString());
        }
    }

c# c# 
Updated 18-Sep-2014
Uttam Misra

Information Technology

More than 18 years of working experience in IT sector. We are here to serve you best.


Message

Leave Comment

Comments

Liked By